package com.guigu.qingcheng.systemservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.guigu.qingcheng.pojo.system.TbMenu;
import com.guigu.qingcheng.systemservice.mapper.TbMenuMapper;
import com.guigu.qingcheng.systemservice.service.TbMenuService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author testjava
 * @since 2020-07-08
 */
@Service
public class TbMenuServiceImpl extends ServiceImpl<TbMenuMapper, TbMenu> implements TbMenuService {
    @Autowired
    private TbMenuService service;

    @Resource
    private TbMenuMapper mapper;
    /**
     * 条件查询
     * @param searchMap， 查询所带条件
     * @return
     */
    @Override
    public List<TbMenu> findList(Map<String, Object> searchMap) {
        QueryWrapper<TbMenu> wapper = queryWrapper(searchMap);
        List<TbMenu> list = service.list(wapper);
        return list;
    }

    /**
     * 分页条件查询
     * @param page 页号
     * @param size 页大小
     * @param searchMap 查询所带条件
     * @return
     */
    @Override
    public Page<TbMenu> findPage(Integer page, Integer size, Map<String, Object> searchMap) {
        Page<TbMenu> pg = new Page<TbMenu>(page, size);
        QueryWrapper<TbMenu> wapper = queryWrapper(searchMap);
        IPage<TbMenu> list = service.page(pg, wapper);
        return pg;
    }

    /**
     * 高级组合查询封装对象
     * @param searchMap
     * @return
     */
    public QueryWrapper<TbMenu> queryWrapper(Map<String, Object> searchMap){
        QueryWrapper<TbMenu> wapper = new QueryWrapper<TbMenu>();
        if(searchMap != null){
            if(searchMap.get("id") != null && !"".equals(searchMap.get("id")))
                wapper.eq("id", searchMap.get("id"));
            if(searchMap.get("name") != null && !"".equals(searchMap.get("name")))
                wapper.like("name", searchMap.get("name"));
            if(searchMap.get("icon") != null && !"".equals(searchMap.get("icon")))
                wapper.like("icon", searchMap.get("icon"));
            if(searchMap.get("url") != null && !"".equals(searchMap.get("url")))
                wapper.like("url", searchMap.get("url"));
            if(searchMap.get("parentId") != null && !"".equals(searchMap.get("parentId")))
                wapper.eq("parent_id", searchMap.get("parentId"));

        }
        return wapper;
    }

    /**
     * 查询全部菜单
     * @return
     */
    @Override
    public List<Map> findAllMenu() {
        List<TbMenu> menuList = service.findList(null);
        return findMenuListByParentId(menuList, "0");
    }

    @Override
    public List<Map> findAllMenu2(String admin) {
        List<TbMenu> menuList = mapper.findList2(admin);
        return findMenuListByParentId(menuList, "0");
    }

    private List<Map> findMenuListByParentId(List<TbMenu> menuList, String parentId){
        List<Map> mapList = new ArrayList<>();
        for( TbMenu menu: menuList ){
            if(menu.getParentId().equals(parentId)){
                Map map = new HashMap();
                map.put("path",menu.getId());
                map.put("title",menu.getName());
                map.put("icon",menu.getIcon());
                map.put("linkUrl",menu.getUrl());
                map.put("children", findMenuListByParentId(menuList, menu.getId()));
                mapList.add(map);
            }
        }
        return mapList;
    }
}
